/*
************************************************************************************************************************
	Creates estimation dataset $workdata/crime with information on:
	- Criminal charges (charge_min)

	Uses estimation dataset:
	- $temp/baseline (see "$data_do/baseline)

	Registers: Variables (Years) 
	- KRSI: pnr, sig_ger1dto, sig_ger7, journr (2000-2006)

************************************************************************************************************************
*/


*******************************************
* Select indiviudals in baseline sample.
* Save temporary file `main'
*******************************************

tempfile main
use pnr foed_dagl year using "$workdata/baseline", clear
keep if year>=2000 & year<=2006
collapse foed_dagl, by(pnr)
save `main'

*******************************************
* Use data on criminal chrages KRSI
* Merge with baseline sample.
* Define age in months at date of crime (age_q)
* Save crime_00_06
*******************************************

forvalues year=2000/2006 {

tempfile krim`year'
use pnr sig_ger1dto sig_ger7 journr using "$rawdata/krsi`year'", clear
duplicates drop pnr journr , force
capture drop _merge

merge m:1 pnr using `main'
capture drop _merge
save  `krim`year''
}
use `krim2000', clear
forvalues year=2001/2006{
append using `krim`year''
}
keep if foed_dagl != .
keep if sig_ger1dto !=.
capture drop _merge
g age=(sig_ger1dto-foed_dagl)/365.25
g age_q=floor( (age-floor(age))*12)*(1/12) + floor(age)
save "$temp/crime_00_06", replace


*******************************************
* Keep one charge per age in months (charge_min).
* Save crime_m_00_06
*******************************************

use "$temp/crime_00_06", clear

g charges=1
tostring sig_ger7, g(sig_ger7_str)
capture drop charge_type
g charge_type=substr(sig_ger7_str,1,2)

collapse (sum) charges (min) charge_min=charges , by(pnr age_q) 
save "$workdat_abj/crime_m_00_06", replace

use "$workdat_abj/crime_m_00_06", clear

*******************************************
* Use baseline sample and merge with crime_m_00_06 on age in months (age_q).
*******************************************

use "$workdata/baseline", clear
keep if year>=2000 & year<=2006
capture drop age_q
g age_q=floor( (age-floor(age))*12)*(1/12) + floor(age)
keep if age_q>=20 & age_q<30
g e=1
replace month=12 if month==0

merge m:1 pnr age_q using "$temp/crime_m_00_06" , keep(master match)
capture drop _merge
replace charges=0 if charges==.
replace charge_min=0 if charge_min==.

g b_month=month(foed_dagl)
g b_year=year(foed_dagl)

collapse charge_min (sum) e, by(pnr age_q koen month year b_month b_year) 

save "$workdata/crime", replace

